package controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import model.Blog;
import model.BlogDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/update")
public class BlogUpdate extends HttpServlet {
    ObjectMapper objectMapper = new ObjectMapper();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf8");
        // 接受更新的博客
        BlogDao blogDao = new BlogDao();
        String blogId = req.getParameter("blogId");
        if (blogId == null || "".equals(blogId)){
            resp.getWriter().write("notFind");
        }
        Blog blog = blogDao.selectOneBlog(Integer.parseInt(blogId));
        if (blog == null) {
            resp.getWriter().write("notFind");
        }
        resp.setContentType("text/json; charset=utf8");
        resp.getWriter().write(objectMapper.writeValueAsString(blog));// 将博客返回
    }
}
